package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.dao.CdnDomainInfoLinkDao;

@Repository
public class CdnDomainInfoLinkDaoImpl implements CdnDomainInfoLinkDao {

	private static final Log log = LogFactory.getLog(CdnDataDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(IMap param){
		//TODO 客户下域名
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select (select cdn.cdnName from t_cdn_cdndata cdn where cdn.cdnId = t.cdnId) as cdnName,t.domainId,t.domain,t.regId,t.timstamp ");
		sql.append("  from t_cdn_customer_service_domain t where 1 = 1");
		sql.append(getQueryConf(param));
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count from t_cdn_customer_service_domain t where 1 = 1");
		sql.append(getQueryConf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(IMap param) {
		String regId = CommonUtils.valueOf(param.get("regId"));
		String domain = CommonUtils.valueOf(param.get("domain"));
		String linkCdnId = CommonUtils.valueOf(param.get("linkCdnId"));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(linkCdnNetId) && StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and exists (select net.domainId from t_cdn_cdnnetinfo net where net.cdnNetId='"+linkCdnNetId+"' and net.cdnId='"+linkCdnId+"' and net.domainId = t.domainId)");
		}
		
		if(StringUtils.isNotBlank(regId)) {
			queryConf.append(" and t.regId like '%"+regId+"%'");
		}
		if(StringUtils.isNotBlank(domain)) {
			queryConf.append(" and t.domain like '%"+domain+"%'");
		}
		if(StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and t.cdnId='"+linkCdnId+"'");
		}
		return queryConf;
	}
}